﻿using System;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Windows.Input;
using Lib.Helper;
using Lib.WarehouseServices;
using MSIACCSL2.Command;
using System.ComponentModel;
using WcfService1.DTO;

namespace Lib.Mail
{
    public class ChooseDestinationViewModel : ViewModelBase
    {
        private WarehouseServices.WarehouseServicesClient warehouseClient;
        private ObservableCollection<WarehouseDTO> _warehouses;
        private WarehouseDTO warehouse;

        public ChooseDestinationViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                //warehouseClient = new WarehouseServicesClient();
                OKCommand = new DelegateCommand(OK, CanOK);
                WarehouseSelected = new WarehouseDTO();
                LoadWarehouse();
            }
        }

        public ObservableCollection<WarehouseDTO> Warehouses
        {
            get { return _warehouses; }
            private set
            {
                if (Warehouses == value) return;
                _warehouses = value;
                FirePropertyChanged("Warehouses");
            }
        }

       

        public WarehouseDTO WarehouseSelected
        {
            get { return warehouse; }
            private set
            {
                if (WarehouseSelected == value) return;
                warehouse = value;
                FirePropertyChanged("WarehouseSelected");
            }
        }

        public ICommand OKCommand { get; set; }

        public bool CanOK(Object param)
        {
            return true;
        }

        public void LoadWarehouse()
        {
            var UserSetting = IsolatedStorageSettings.ApplicationSettings;
            Warehouses = UserSetting["warehouse"] as ObservableCollection<WarehouseDTO>;
            //warehouseClient.GetAllWarehouseCompleted += new EventHandler<GetAllWarehouseCompletedEventArgs>(warehouseClient_GetAllWarehouseCompleted);
            //warehouseClient.GetAllWarehouseAsync();
        }

        void warehouseClient_GetAllWarehouseCompleted(object sender, GetAllWarehouseCompletedEventArgs e)
        {
            warehouseClient.GetAllWarehouseCompleted -= warehouseClient_GetAllWarehouseCompleted;
            Warehouses = e.Result;

        }

        public void OK(Object param)
        {
            EventAggregator.PublishMessage("SendDestination",param as WarehouseDTO);
        }
    }
}
